package org.yunshang.beltbody.controller;

import java.io.IOException;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.yunshang.common.excel.core.CustomImageModifyHandler;
import org.yunshang.common.excel.utils.ExcelUtil;
import org.yunshang.common.idempotent.annotation.RepeatSubmit;
import org.yunshang.common.log.annotation.Log;
import org.yunshang.common.web.core.BaseController;
import org.yunshang.common.mybatis.core.page.PageQuery;
import org.yunshang.common.core.domain.R;
import org.yunshang.common.core.validate.AddGroup;
import org.yunshang.common.core.validate.EditGroup;
import org.yunshang.common.log.enums.BusinessType;
import org.yunshang.beltbody.domain.vo.TBeltBodyVo;
import org.yunshang.beltbody.domain.bo.TBeltBodyBo;
import org.yunshang.beltbody.service.ITBeltBodyService;
import org.yunshang.common.mybatis.core.page.TableDataInfo;

/**
 * 带身信息
 *
 * @author Lion Li
 * @date 2024-03-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/beltBody")
public class TBeltBodyController extends BaseController {

    private final ITBeltBodyService tBeltBodyService;

    /**
     * 查询带身信息列表
     */
    @SaCheckPermission("business:beltBody:list")
    @GetMapping("/list")
    public TableDataInfo<TBeltBodyVo> list(TBeltBodyBo bo, PageQuery pageQuery) {
        return tBeltBodyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出带身信息列表
     */
    @SaCheckPermission("business:beltBody:export")
    @Log(title = "带身信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TBeltBodyBo bo, HttpServletResponse response) throws IOException {
        List<TBeltBodyVo> list = tBeltBodyService.queryList(bo);
//        ExcelUtil.exportExcel(list, "带身信息", TBeltBodyVo.class, response);
        String outputStream = null;
        EasyExcel.write(response.getOutputStream(), TBeltBodyVo.class)
            .autoCloseStream(true)
            .registerWriteHandler(new CustomImageModifyHandler())
            .sheet("sheet1").doWrite(list);

    }

    /**
     * 获取带身信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:beltBody:query")
    @GetMapping("/{id}")
    public R<TBeltBodyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tBeltBodyService.queryById(id));
    }

    /**
     * 新增带身信息
     */
    @SaCheckPermission("business:beltBody:add")
    @Log(title = "带身信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TBeltBodyBo bo) {
        return toAjax(tBeltBodyService.insertByBo(bo));
    }

    /**
     * 修改带身信息
     */
    @SaCheckPermission("business:beltBody:edit")
    @Log(title = "带身信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TBeltBodyBo bo) {
        return toAjax(tBeltBodyService.updateByBo(bo));
    }

    /**
     * 删除带身信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:beltBody:remove")
    @Log(title = "带身信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tBeltBodyService.deleteWithValidByIds(List.of(ids), true));
    }
}
